<?php
/**
 * Translate given key
 * @param String $original
 * @param String $translation
 */
function __($key, $return=false) {
	$translate = View::translate($key);
	if($return){
		return $translate;
	}else{
		echo $translate;
	}
}
/**
 * Test Class Existance before loading it
 * @param String $ClassName
 */
function __autoload($className) {
	$filename = class2file($className);

	$found = false;
	$libraries = array(
		"lib"				=> "%s.class.php",
		"app/controllers"	=> "%s.controller.php",
		"app/models"		=> "%s.php"
	);
	
	foreach($libraries as $lib_path=>$lib_filename) {
		$dir = dirname(__FILE__)."/..";
		$path = "$dir/$lib_path/".sprintf($lib_filename, $filename);
		
		if(file_exists($path)) {
			require_once "$path";
			$found = true;
			break;
		}
	}
}
/**
 * Convert Class name (camelcase) to file name
 * @param String $ClassName
 */
function class2file($className) {
	$ret = "";
	for($i=0; $i<strlen($className); $i++) {
		$c = substr($className, $i, 1);
		if($c==strtoupper($c) && $i>0) {
			$c = "_$c";
		}
		$ret .= strtolower($c);
	}
	return $ret;
}
/**
 * Convert file name to Class Name (camelcase)
 * @param String $file_name
 */
function file2class($filename, $space=false) {
	$ret = preg_replace('/_(.)/e', "strtoupper('".($space ? " " : "")."\\1')", preg_replace('/[^a-z0-9]/i', '_', $filename));
	$ret = preg_replace('/^(.)/e', "strtoupper('\\1')", $ret);
	return $ret;
}

/**
 * Dump variable into output
 * @param Mixed $variable
 */
function _dump($var) {
	echo '<pre>';
	if(is_array($var) || is_object($var)) {
		print_r($var);
	}else if(is_bool($var) || is_null($var) || empty($var)) {
		var_dump($var);
	}else{
		echo $var;
	}
	echo '</pre>';
}

/**
 * Redirect to URL
 * @param String $URL
 */
function redirect($url=null){
	ob_clean();

	if(!$url){
		$url = $_SERVER["HTTP_REFERER"];
	}
	if(!$url){
		$url = "/";
	}

	header("Location: $url");
	die;
}

/**
 * Convert text to Title case
 * @param String $text
 * @return String $Return
 */
function strtotitle($str){
	$ret = "";
	foreach(explode(" ", $str) as $word){
		$ret .= " ". mb_strtoupper(mb_substr($word, 0, 1), "UTF-8") . mb_strtolower(mb_substr($word, 1), "UTF-8");
	}
	return preg_replace('/^\s/', "", $ret);
}

/**
 * Encode string for URL
 * @param String $text
 * @return String $encoded_text
 */
function urlize($text){
	$ret = preg_replace(array('/č|ć/','/š/','/ž/','/đ/'), array('c','s','z','dj'), mb_strtolower($text, "UTF-8"));
	$ret = preg_replace('/[\s\t_\/\?\^&\*\+]/i', "-", $ret);
	return preg_replace('/-{2,}/', "-", $ret);
}
/**
 * Decode string from URLized string
 * @param String $text
 * @return String $decoded_text
 */
function deurlize($text, $replace=" "){
	return str_replace("-", $replace, $text);
}

/**
 * Validate an e-mail adress
 * @param String $email
 * @return Bool
 */
function is_email($email){
	return preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $email) ? true : false;
}

/**
 * Cycle passed values
 * @param String $even_value
 * @param String $odd_value
 */
function cycle($even,$odd){
	$i = (int)$_SESSION["cycle_iter"];
	$_SESSION["cycle_iter"]++;
	return $i%2 ? $odd : $even;
}

/**
 *Format string
 * @param String $string
 * @param String $type
 */
function format($string, $type){
	$ret = $string;
	switch($type){
		case "currency":
			return number_format((float)$string, 2, ",", ".") . " " . CURRENCY_LABEL;
			break;
		case "float":
			return number_format((float)$string, 2, ",", ".");
			break;
	}
	return $string;
}

function empty_dir($dirname, $except=array(), $remove_dir=false){
	if($dir=opendir($dirname)){
		while ($f = readdir($dir)){
			if(!preg_match('/^\.+$/', $f) && !in_array($f, $except)){
				unlink("$dirname/$f");
			}
		}
		if($remove_dir){
			rmdir($dirname);
		}
	}
}
?>